package com.prs.crm.action.sys;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.sys.Menu;
import com.prs.crm.domain.sys.Module;
import com.prs.crm.service.sys.MenuService;
import com.prs.crm.service.sys.ModuleService;

public class MenuAction extends BaseAction {

	private static final long serialVersionUID = 3254208753884059684L;

	private Menu menu;

	private Menu parent;
	
	private ModuleService moduleService;

	private MenuService menuService;

	private PaginationSupport pagination;

	private Integer[] menuIds;
    
	private Collection<Module> modules;
	
	private Collection<TreeNode> nodes;

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenuIds(Integer[] menuIds) {
		this.menuIds = menuIds;
	}

	public Integer[] getMenuIds() {
		return menuIds;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	@Action("listMenu")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Menu.class);
		pagination = getPaginationHelper().prepare(pagination);
		pagination.setOrders(new Order[] { Order.asc("parent"),
				Order.asc("sequence") });
		this.setPagination(this.getMenuService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectMenu")
	public String selectMenu() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Menu.class);
		pagination = getPaginationHelper().prepare(pagination);
		pagination.setOrders(new Order[] { Order.asc("parent"),Order.asc("sequence") });
		this.setPagination(this.getMenuService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("addChildMenu")
	public String add() {
		return SUCCESS;
	}
	@Action("editMenu")
	public String edit() {
		this.setModules(this.getModuleService().getAllModule());
		return SUCCESS;
	}

	@Action(value = "saveMenu", results = {
			@Result(name = "success", type = "redirect", location = "listMenu"),
			@Result(name = "error", type = "chain", location = "editMenu") })
	public String save() {
		if (menu.getText() == null || "".equals(menu.getText().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		this.getMenuService().save(menu);
		return SUCCESS;
	}

	@Action(value = "removeMenu", results = { @Result(name = "success", type = "redirect", location = "listMenu") })
	public String remove() {
		this.getMenuService().batchRemove(menuIds);
		return SUCCESS;
	}

	@Action(value = "adjustUp", results = { @Result(name = "success", type = "redirect", location = "listMenu") })
	public String adjustUp() {
		this.getMenuService().adjustUp(menu);
		return SUCCESS;
	}

	@Action(value = "adjustDown", results = { @Result(name = "success", type = "redirect", location = "listMenu") })
	public String adjustDown() {
		this.getMenuService().adjustDown(menu);
		return SUCCESS;
	}

	@Action(value = "loadMenuTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadTreeNodes() {
		nodes = new ArrayList<TreeNode>();
		Collection<Menu> rootMenus = this.getMenuService().getRootMenus();
		for (Menu menu : rootMenus) {
			TreeNode node = new TreeNode();
			node.setId(menu.getId().toString());
			node.setText(menu.getText());
			addChildNodes(menu, node);
			nodes.add(node);
		}
		return SUCCESS;
	}

	private void addChildNodes(Menu menu, TreeNode parentNode) {
		if (menu.getChilds() != null) {
			for (Menu childMenu : menu.getChilds()) {
				TreeNode node = new TreeNode();
				node.setId(childMenu.getId().toString());
				node.setText(childMenu.getText());
				parentNode.addChildNode(node);
				addChildNodes(childMenu, node);
			}
		}
	}

	@Action(value = "loadChildTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadChildTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<Menu> menus = this.getMenuService().getChildMenu(menu);
		for (Menu menu : menus) {
			TreeNode node = new TreeNode();
			node.setId(menu.getId().toString());
			node.setText(menu.getText());
			addChildNodes(menu, node);
			nodes.add(node);
		}
		return SUCCESS;
	}

	public void setParent(Menu parent) {
		this.parent = parent;
	}

	public Menu getParent() {
		return parent;
	}

	public Collection<Module> getModules() {
		return modules;
	}

	public void setModules(Collection<Module> modules) {
		this.modules = modules;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}
	
}
